<template>
  <div class="login-container">
    <div class="logo" />
    <div class="form">
      <h1>HR后台登录</h1>
      <el-card shadow="never" class="login-card">
        <!--登录表单-->
        <!-- 全局注册实例ref属性  loginForm是存放数据 loginRules存放校验规则 -->
        <el-form ref="form" :model="loginForm" :rules="loginRules">
          <!-- prop声明字段 -->
          <!-- 输入框 -->
          <el-form-item prop="mobile">
            <el-input type="text" v-model="loginForm.mobile" placeholder="请输入账号" :maxlength="11"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input type="password" v-model="loginForm.password" placeholder="请输入密码"></el-input>
          </el-form-item>
          <!-- 用户平台使用协议 -->
          <!-- 传入prop属性 -->
          <el-form-item prop="isAgree">
            <el-checkbox v-model="loginForm.isAgree">平台用户使用协议</el-checkbox>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" style="width:350px" @click="login">登录</el-button>
          </el-form-item>
          <el-button @click="testAjax">测试数据</el-button>
        </el-form>
      </el-card>
    </div>
  </div>
</template>
<script>
import request from "@/utils/request.js";
export default {
  name: "Login",
  data() {
    return {
      loginForm: {
        mobile: "", //手机号
        password: "", //密码存放
        isAgree: false //控制勾选单选框
      },
      // 校验规则,规则数组包对象[{},{}],多条规则用多个对象隔开
      loginRules: {
        //手机号
        mobile: [
          {
            required: true, //要求必须填写
            message: "请输入手机号", //提示信息
            trigger: "blur", //触发方式是blur
            max: 11
          },
          {
            // 规则正则表达式
            pattern: /^1[3-9]\d{9}$/, //手机号匹配规则
            message: "手机格式不正确", //提示信息
            trigger: "blur" //触发方式
          }
        ], //密码存放
        password: [
          {
            required: true, //是否是必须输入的字段
            message: "请输入密码", //提示信息
            trigger: "blur" //触发方式
          },
          {
            min: 6, //密码长度最小
            max: 16,
            message: "密码格式不正确",
            trigger: "blur"
          }
        ],
        isAgree: [
          {
            //自定义校验函数
            // rule表示校验规则，value表示检查数据true/false，callback表示必须执行的回调函数
            validator: (rule, value, callback) => {
              // 使用三元表达式校验数据
              // 校验数据成功执行callback(),失败执行callback(new Error('错误信息'))
              value ? callback() : callback(new Error("请勾选平台协议"));
            }
          }
        ] //控制勾选单选框
      }
    };
  },
  methods: {
    login() {
      //校验整个表单this.$refs.validate是拿去整个实例检验表单
      // 调用validate(isok=>{})函数，该函数中还有回调函数判断全局表单校验成功或失败
      this.$refs.form.validate(isOk => {
        if (isOk) {
          alert("校验成功");
          // 调用actions发送请求获取token，将token实现页面共享,将个人数据传入vuex中发请求
          this.$store.dispatch("user/logIn", this.loginForm);
        }
      });
    },
    testAjax(){
      request.post('/sys/login',{
        mobile:'15181066396',
        password:'123123'
      })
    }

  }
};
</script>
<style lang="scss">
.login-container {
  display: flex;
  align-items: stretch;
  height: 100vh;
  .logo {
    flex: 3;
    background: rgba(38, 72, 176) url(../../assets/common/login_back.png)
      no-repeat center / cover;
    border-top-right-radius: 60px;
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    justify-content: center;
    padding: 0 100px;
    .icon {
      background: url(../../assets/common/logo.png) no-repeat 70px center /
        contain;
      width: 300px;
      height: 50px;
      margin-bottom: 50px;
    }
    p {
      color: #fff;
      font-size: 18px;
      margin-top: 20px;
      width: 300px;
      text-align: center;
    }
  }
  .form {
    flex: 2;
    display: flex;
    flex-direction: column;
    justify-content: center;
    padding-left: 176px;
    .el-card {
      border: none;
      padding: 0;
    }
    h1 {
      padding-left: 20px;
      font-size: 24px;
    }
    .el-input {
      width: 350px;
      height: 44px;
      .el-input__inner {
        background: #f4f5fb;
      }
    }
    .el-checkbox {
      color: #606266;
    }
  }
}
</style>